/*
 *  Copyright 2010 johan.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package nu.zoom.catonine.swing.tail;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.Icon;
import javax.swing.JToolBar;

import nu.zoom.swing.action.AbstractTypedAction;
import nu.zoom.ui.Resources;
import nu.zoom.ui.Resources.ResourceNotFoundException;

/**
 * Action to toggle the tailing window toolbar.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 */
public class ToggleToolbarAction extends AbstractTypedAction {
	private static final long serialVersionUID = 1L;
	private final JToolBar toolbar;
	private final String hideToolbarText;
	private final String showToolbarText;
	private final Icon hideToolbarIcon;
	private final Icon showToolbarIcon;

	public ToggleToolbarAction(final JToolBar toolbar, final Resources localizedResources)
			throws ResourceNotFoundException {
		this.toolbar = toolbar;
		this.hideToolbarText = localizedResources.getMessage("nu.zoom.catonine.tail.toolbar.hide");
		this.showToolbarText = localizedResources.getMessage("nu.zoom.catonine.tail.toolbar.show");
		this.hideToolbarIcon = localizedResources.getIcon("nu.zoom.catonine.tail.toolbar.hide.icon");
		this.showToolbarIcon = localizedResources.getIcon("nu.zoom.catonine.tail.toolbar.show.icon");
		setToolTip(localizedResources.getMessage("nu.zoom.catonine.tail.toolbar.tt"));
		setName(this.hideToolbarText);
		setIcon(hideToolbarIcon);
		setMnemonicKey(KeyEvent.VK_L);
		setAcceleratorKey(KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (toolbar.isVisible()) {
			toolbar.setVisible(false);
			setName(showToolbarText);
			setIcon(showToolbarIcon);
		} else {
			toolbar.setVisible(true);
			setName(hideToolbarText);
			setIcon(hideToolbarIcon);
		}
	}
}
